﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.WPF
{
    public static class StartupUtil
    {
        /// <summary>
        /// 将参数字符串解析为参数
        /// <para>/p1 p1Value /p2 p2Value /p3 /p4</para>
        /// </summary>
        /// <param name="paramString"></param>
        /// <returns></returns>
        public static List<StartupParam> ParseParameter(string paramString)
        {
            List<StartupParam> paramList = new List<StartupParam>();

            string[] segmentArr = paramString.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            for (int index = 0; index < segmentArr.Length; index++)
            {
                string[] segmentKVArr = segmentArr[index].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (segmentKVArr.Length == 1)
                {
                    StartupParam param = new StartupParam();
                    param.Name = segmentKVArr[0];
                    paramList.Add(param);
                }
                else
                {
                    StartupParam param = new StartupParam();
                    param.Name = segmentKVArr[0];
                    param.Value = segmentKVArr[1];
                    paramList.Add(param);
                }
            }

            return paramList;
        }

        /// <summary>
        /// <para>/p1</para>
        /// <para>p1Value</para>
        /// <para>/p2</para>
        /// <para>p2Value</para>
        /// <para>/p3</para>
        /// </summary>
        /// <param name="paramArray"></param>
        /// <returns></returns>
        public static StartupParamCollection Parse(string[] paramArray)
        {
            StartupParamCollection paramList = new StartupParamCollection();

            StartupParam param = null;
            for (int index = 0; index < paramArray.Length; index++)
            {
                string segment = paramArray[index];
                if (segment.StartsWith("/"))
                {
                    param = new StartupParam();
                    param.Name = segment.Replace("/", string.Empty).ToUpper();
                    paramList.Add(param);
                }
                else
                {
                    param.Value = segment.ToUpper();
                }
            }

            return paramList;
        }
    }
}
